<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="contactName">
        <el-input v-model="queryParams.contactName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="关联账号" prop="userAccount">
        <el-input v-model="queryParams.userAccount" placeholder="请输入关联账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="contactPhone">
        <el-input v-model="queryParams.contactPhone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="角色" prop="appRoleId">
        <el-select v-model="queryParams.appRoleId" placeholder="请选择角色" clearable :popper-append-to-body="false"
          class="product-style">
          <el-option v-for="dict in approleOptions" :key="dict.id" :label="dict.roleName" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择所属项目" clearable filterable
          :popper-append-to-body="false" class="product-style">
          <el-option v-for="dict in projectOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="资源利用化企业" prop="disposalLandId">
        <el-select v-model="queryParams.disposalLandId" placeholder="请选择资源利用化企业" clearable filterable>
          <el-option v-for="dict in disposalLandOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="清运公司" prop="transportCompanyId">
        <el-select v-model="queryParams.transportCompanyId" placeholder="请选择所属公司" filterable clearable>
          <el-option v-for="dict in transportOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['appManage:contact:add']">新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['appManage:contact:edit']">修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['appManage:contact:remove']">批量删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['appManage:contact:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contactList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="contactName" />
      <el-table-column label="关联账号" align="center" prop="userAccount" />
      <el-table-column label="手机号" align="center" prop="contactPhone" />
      <el-table-column label="角色" align="center" prop="appRoleName" />
      <el-table-column label="所属项目" align="center" prop="projectName" />
      <!-- <el-table-column label="资源利用化企业" align="center" prop="disposalLandName" /> -->
      <el-table-column label="清运公司" align="center" prop="transportCompanyName" />
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['appManage:contact:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['appManage:contact:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改联系人管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入名称" maxlength="10" />
        </el-form-item>
        <el-form-item label="手机号" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入手机号" maxlength="11" />
        </el-form-item>
        <el-form-item label="app角色" prop="appRoleId">
          <el-select v-model="form.appRoleId" placeholder="请选择角色" clearable filterable :popper-append-to-body="false"
            class="product-style">
            <el-option v-for="dict in approleOptions" :key="dict.id" :label="dict.roleName" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择所属项目" clearable filterable :popper-append-to-body="false"
            class="product-style">
            <el-option v-for="dict in projectOptions" :key="dict.id" :label="dict.name" :value="dict.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资源利用化企业" prop="disposalLandId">
          <el-select v-model="form.disposalLandId" placeholder="请选择所属资源利用化企业" clearable filterable>
            <el-option v-for="dict in disposalLandOptions" :key="dict.id" :label="dict.name" :value="dict.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="清运公司" prop="transportCompanyId">
          <el-select v-model="form.transportCompanyId" placeholder="请选择所属公司" clearable filterable>
            <el-option v-for="dict in transportOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="司机" prop="driverId">
          <el-select v-model="form.driverId" placeholder="请选择司机" clearable filterable>
            <el-option v-for="dict in driverOptions" :key="dict.id" :label="dict.name" :value="parseInt(dict.id)" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContact, getContact, delContact, addContact, updateContact } from "@/api/appManage/contact";
import { allCompany } from "@/api/company/company";
import { allProject } from "@/api/projectManage/project";
import { allDisposalLand } from "@/api/disposal/disposalLand";
import { allApprole } from "@/api/appManage/approle";
import { allDriver } from "@/api/carManage/driver";

export default {
  name: "Contact",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联系人管理表格数据
      contactList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        contactName: null,
        contactPhone: null,
        appUserId: null,
        appRoleId: null,
        disposalLandId: null,
        projectId: null,
        transportCompanyId: null,
        userAccount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contactName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: this.$ConstantUtil.REG_PHONE,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
      },
      // 清运公司选项
      transportOptions: [],
      // 项目选项
      projectOptions: [],
      // 资源利用化企业选项
      disposalLandOptions: [],
      // 角色选项
      approleOptions: [],
      // 司机选项
      driverOptions: [],
    };
  },
  created () {
    this.getList();
    this.getTransportCompanyList();
    this.getProjectList();
    this.getDisposalLandList();
    this.getApproleList();
    this.getDriverList();
  },
  methods: {
    // 清运公司列表
    getTransportCompanyList () {
      allCompany().then(response => {
        this.transportOptions = response.data;
      })
    },
    // 项目列表
    getProjectList () {
      allProject().then(response => {
        this.projectOptions = response.data;
      })
    },
    getDisposalLandList () {
      allDisposalLand().then(response => {
        this.disposalLandOptions = response.data;
      })
    },
    getApproleList () {
      allApprole().then(response => {
        this.approleOptions = response.data;
      })
    },
    getDriverList () {
      allDriver().then(response => {
        this.driverOptions = response.data;
      })
    },
    /** 查询联系人管理列表 */
    getList () {
      this.loading = true;
      listContact(this.queryParams).then(response => {
        this.contactList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        contactName: null,
        contactPhone: null,
        appUserId: null,
        appRoleId: null,
        disposalLandId: null,
        projectId: null,
        transportCompanyId: null,
        driverId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加联系人管理";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getContact(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改联系人管理";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContact(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContact(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delContact(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('appManage/contact/export', {
        ...this.queryParams
      }, `联系人管理_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
